package com.example.fileserver_API.utils;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@Component
public class RedisUtils {
    @Resource
    private RedisTemplate<String,String> redisTemplate;


    /**
     * 读取数据
     */
    public String getString(final String key) {
        String str = redisTemplate.opsForValue().get(key);
        System.out.println("Redis读取数据："+str);
        return str;
    }

    /**
     * 写入数据
     */
    public boolean setString(final String key, String value, long time) {
        try {
            System.out.println("Redis设置String值：Key:"+key+"，值："+value+"，时间："+time);
            redisTemplate.opsForValue().set(key, value);
            if (time>0)
                redisTemplate.expire(key, time, TimeUnit.SECONDS);
            return true;
        } catch (Exception e) {
            System.out.println("Redis设置String值发生错误："+e.getMessage());
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 更新数据
     */
    public boolean getAndSetString(final String key, String value) {
        try {
            System.out.println("Redis修改String值：Key:"+key+"，值："+value);
            redisTemplate.opsForValue().getAndSet(key, value);
            return true;
        } catch (Exception e) {
            System.out.println("Redis修改String值发生错误："+e.getMessage());
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 删除数据
     */
    public boolean delete(final String key) {
        try {
            System.out.println("Redis删除数据："+key);
            redisTemplate.delete(key);
            return true;
        } catch (Exception e) {
            System.out.println("Redis删除数据发生错误："+e.getMessage());
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 设置Hash缓存
     * @param hkey 大key
     * @param key 小key
     * @param value 值
     * @return boolean
     */
    public boolean SetHashValue(String hkey,String key,Object value){
        try {
            System.out.println("Redis设置Hash值：大Key:"+hkey+"，小Key:"+key+"，值："+value);
            redisTemplate.boundHashOps(hkey).put(key, value);
            return true;
        }
        catch (Exception e){
            System.out.println("Redis设置Hash值的时候发生错误："+e.getMessage());
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 获取具体的Hash缓存
     * @param hkey 大key
     * @param key 小key
     * @return String
     */
    public Object GetHashValue(String hkey,String key){
        Object value = redisTemplate.boundHashOps(hkey).get(key);
        System.out.println("Redis读取数据(Hash具体key的值)："+value);
        return value;
    }

    /**
     * 获取Hash大key下的所有value
     * @param hkey 大key
     * @return List<Object>
     */
    public List<Object> GetHashValue(String hkey){
        List<Object> values = redisTemplate.boundHashOps(hkey).values();
        System.out.println("Redis读取数据(Hash大key下的值)："+values);
        return values;
    }

    /**
     * 获取某个Hash中大key下的所有key
     * @param hkey 大key
     * @return Set
     */
    public Set<Object> GetHashKeys(String hkey){
        Set<Object> s = redisTemplate.boundHashOps(hkey).keys();
        System.out.println("Redis读取数据(Hash大key下所有的key)："+s);
        return s;
    }

    /**
     * 删除Hash中某个小key
     * @param hkey 大key
     * @param key 小key
     * @return boolean
     */
    public boolean DeleteHashKey(String hkey,String key){
        try {
            System.out.println("Redis删除数据(Hash)：大Key："+hkey+"，小Key："+key);
            redisTemplate.boundHashOps(hkey).delete(key);
            return true;
        }
        catch (Exception e){
            System.out.println("Redis删除数据发生错误(Hash)："+e.getMessage());
            return false;
        }
    }
}

